package com.yan.controller;

import com.alibaba.fastjson.JSON;
import com.rabbitmq.client.*;
import com.yan.config.ConsumerListener;
import com.yan.config.DlxListener;
import com.yan.config.RabbitConstants;
import com.yan.entity.Result;
import com.yan.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.*;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.connection.Connection;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * TODO
 *
 * @Author: 汪焰
 * @Date: 2021/7/8 22:07
 */
@Slf4j
@RestController
public class DelayedController {

    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private ConsumerListener consumerListener;
    @Autowired
    private DlxListener dlxListener;

    /**
     * http://localhost:8085/createOrder/纸巾
     */
    @GetMapping("/createOrder/{productName}")
    public Result createOrder(@PathVariable String productName) throws Exception {
        Assert.notNull(productName, "购买的产品名称 不能为空");

        int expire = 20;

        String createDate = DateUtils.getDateStr();
        String expireDate = DateUtils.parseDateToStr(DateUtils.addSecond(new Date(), expire));

        Result result = new Result(true, productName, createDate, expireDate);


        MessageProperties msgPro = MessagePropertiesBuilder.newInstance()
                //消息过期时间 20秒
                .setHeader("x-delay", expire * 1000)
                .setContentEncoding("utf-8")
                .build();

        Message msg = MessageBuilder.withBody((JSON.toJSONString(result)).getBytes())
                .andProperties(msgPro)
                .build();

        CorrelationData cd = new CorrelationData();
        cd.setId("createOrder 接口的 id");
        Message message1 = new Message("这是 createOrder 的响应".getBytes("utf-8"), new MessageProperties());
        cd.setReturnedMessage(message1);
        //消息发送 成功
        rabbitTemplate.convertAndSend(RabbitConstants.ORDER_QUEUE_EXCHANGE, RabbitConstants.ORDER_QUEUE_ROUTING_KEY, msg, cd);
        //消息发送 失败
//        rabbitTemplate.convertAndSend("sdfsdf", RabbitConstants.ORDER_QUEUE_ROUTING_KEY, msg, cd);
        System.out.println("接口调用完毕！");
        return result;
    }

    /**
     * http://localhost:8085/confirmPay/纸巾
     */
    @GetMapping("/confirmPay/{productName}")
    public Result confirmPay(@PathVariable String productName) throws Exception {
        Assert.notNull(productName, "购买的产品名称 不能为空");
        consumerListener.productNames.add(productName);
        Thread.sleep(2000);
        System.out.println("接口调用完毕！");
        if (consumerListener.success.remove(productName)) {
            return new Result(true);
        }
        return new Result(false);
    }

    /**
     * http://localhost:8085/orderList
     */
    @GetMapping("/orderList")
    public Set<Result> orderList() throws Exception {
        return dlxListener.dlx;
    }

}
